package com.zdxlz.fcmp.upms.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zdxlz.fcmp.common.core.constant.enums.OperateTypeEnum;
import com.zdxlz.fcmp.common.core.util.R;
import com.zdxlz.fcmp.common.log.annotation.LogRecord;
import com.zdxlz.fcmp.common.mybatis.enums.MenuTypeEnum;
import com.zdxlz.fcmp.common.security.util.UserLoginInfoUtil;
import com.zdxlz.fcmp.upms.api.dto.SystemMenuDto;
import com.zdxlz.fcmp.upms.api.entity.SystemMenu;
import com.zdxlz.fcmp.upms.api.service.ISystemMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import static com.zdxlz.fcmp.common.core.constant.CommonConstants.*;

/**
 * 系统菜单控制层
 *
 * @author aman
 * @since 2025-01-15 16:59:00
 */
@RestController
@Tag(name = "菜单管理模块")
@LogRecord(module = "菜单管理模块")
@RequestMapping("/api/v1/sys/menu")
public class SystemMenuController {

    @Resource
    private ISystemMenuService systemMenuService;

    /**
     * @Description：查询菜单路由名称列表
     * @Author：aman
     * @Date：2025/1/17 09:47
     */
    @GetMapping("/routeName")
    @Operation(summary = "查询菜单路由列表")
    @LogRecord(module = "查询菜单路由列表", value = "查询所有菜单的路由信息，返回字符串集合列表", operType = OperateTypeEnum.SELECT)
    public R queryRouteNameList() {
        List<String> routeNameList = systemMenuService.list(new LambdaQueryWrapper<SystemMenu>().eq(SystemMenu::getMenuType, MenuTypeEnum.MENU))
                .stream().map(SystemMenu::getRouteName).toList();
        return R.ok(routeNameList, OPERATION_SUCCESS);
    }

    /**
     * @Description：查询菜单列表
     * @Author：aman
     * @Date：2025/1/17 09:47
     */
    @GetMapping
    @Operation(summary = "查询菜单")
    @LogRecord(module = "查询菜单", value = "查询所有菜单信息", operType = OperateTypeEnum.SELECT)
    public R queryMenuList() {
        return R.ok(systemMenuService.queryMenuList(null), OPERATION_SUCCESS);
    }

    /**
     * @Description：添加菜单信息
     * @Author：aman
     * @Date：2025/1/17 09:48
     */
    @PostMapping
    @Operation(summary = "新增菜单")
    @LogRecord(module = "新增菜单", value = "新增菜单必要条件，菜单名称【{}】，路由名称：【{}】，权限标识：【{}】", field = {"menuName", "routeName", "permission"}, db = true, operType = OperateTypeEnum.INSERT)
    public R insertMenu(@RequestBody @Validated SystemMenu systemMenu) {
        systemMenu.setTenantId(UserLoginInfoUtil.getUserInfo().getTenantId());
        return R.ok(systemMenuService.insertMenu(systemMenu), CREATE_SUCCESS);
    }

    /**
     * @Description：更新菜单信息
     * @Author：aman
     * @Date：2025/1/17 09:49
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新菜单")
    @LogRecord(module = "更新菜单", value = "更新菜单必要条件，菜单ID【{}】，菜单名称【{}】，路由名称：【{}】，权限标识：【{}】", field = {"id", "menuName", "routeName", "permission"}, db = true, operType = OperateTypeEnum.UPDATE)
    public R updateMenu(@PathVariable Integer id, @RequestBody @Validated SystemMenu systemMenu) {
        systemMenu.setId(id);
        systemMenu.setTenantId(UserLoginInfoUtil.getUserInfo().getTenantId());
        return R.ok(systemMenuService.updateMenu(systemMenu), UPDATE_SUCCESS);
    }

    /**
     * @Description：通过id批量更新菜单状态
     * @Author：aman
     * @Date：2025/1/17 09:49
     */
    @PutMapping("/update/status")
    @Operation(summary = "批量更新菜单状态")
    @LogRecord(module = "批量更新菜单状态", value = "更新菜单必要条件，菜单ID【{}】，菜单状态【{}】", field = {"ids", "status"}, db = true, operType = OperateTypeEnum.DELETE)
    public R updateMenuStatus(@RequestBody SystemMenuDto dto) {
        return R.ok(systemMenuService.updateMenuStatus(dto), UPDATE_SUCCESS);
    }

    /**
     * @Description：删除菜单信息
     * @Author：aman
     * @Date：2025/1/17 09:49
     */
    @DeleteMapping
    @Operation(summary = "删除菜单")
    @LogRecord(module = "删除菜单", value = "删除菜单必要条件，菜单ID【{}】", field = {"ids"}, db = true, operType = OperateTypeEnum.DELETE)
    public R deleteMenu(@RequestBody List<Integer> ids) {
        return R.ok(systemMenuService.deleteMenuByIds(ids), DELETE_SUCCESS);
    }
}
